<?php
/**
* A DHTML menu component for Joomla
* @version 1.6.3
* @package ngmenufree
* @copyright (C) 2005 - 2012 by Georg Lorenz
* @license Released under the terms of the GNU General Public License
**/

/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * @package		NGMenu
 * @subpackage	mod_ngmenu
 * @since		1.6
 */
class modNGMenuHelper
{
	/**
	 * Get a list of the menu items.
	 *
	 * @return	array
	 * @since	1.6
	 */
	static function getItems(&$module)
	{
		// Initialise variables.
		$list		= array();
		$db			= JFactory::getDbo();
		$user		= JFactory::getUser();
		$app		= JFactory::getApplication();
		$menu		= $app->getMenu();
		$active		= $menu->getActive();
		$active_id	= isset($active) ? $active->id : $menu->getDefault()->id;
		$path		= isset($active) ? $active->tree : array();
		
		
		// Get the menu type
		$query = $db->getQuery(true);
		$query->select('name');
		$query->from('#__ngmenu');
		$query->where("module_id='".$module->id."'");
		$db->setQuery($query);
//		echo $db->explain();
		$menutype = $db->loadResult();

		$items = $menu->getItems('menutype', $menutype);

		if ($items) {
			foreach($items as $i => $item){
				$item->parent = (boolean) $menu->getItems('parent_id', (int) $item->id, true);

				$item->active		= false;
				$item->flink = $item->link;

				switch ($item->type)
				{
					case 'separator':
						// No further action needed.
						continue;

					case 'url':
						if ((strpos($item->link, 'index.php?') === 0) && (strpos($item->link, 'Itemid=') === false)) {
							// If this is an internal Joomla link, ensure the Itemid is set.
							$item->flink = $item->link.'&Itemid='.$item->id;
						}
						break;

					case 'alias':
						// If this is an alias use the item id stored in the parameters to make the link.
						$item->flink = 'index.php?Itemid='.$item->params->get('aliasoptions');
						break;

					default:
						$router = JSite::getRouter();
						if ($router->getMode() == JROUTER_MODE_SEF) {
							$item->flink = 'index.php?Itemid='.$item->id;
						}
						else {
							$item->flink .= '&Itemid='.$item->id;
						}
						break;
				}

				if (strcasecmp(substr($item->flink, 0, 4), 'http') && (strpos($item->flink, 'index.php?') !== false)) {
					$item->flink = JRoute::_($item->flink, true, $item->params->get('secure'));
				}
				else {
					$item->flink = JRoute::_($item->flink);
				}
				
				$item->title = htmlspecialchars($item->title);
			}

		}

		return $items;
	}

	public static function isGoogleFont($font_name=null)
	{
		if (!$font_name) return false;
		$font_name = str_replace('+', ' ', $font_name);
		$font_name = explode('|', $font_name);
		
		$database =& JFactory::getDBO();
		$select = $database->getQuery(true);
		$select->select('*');
		$select->from('#__ngmenu_fonts');
		$select->where("name='".$font_name[0]."'");
		$database->setQuery($select);
		$database->query();
		if($database->getNumRows()){
			return true;
		}else{
			return false;
		}
	}

	public static function prepareFontFamily($font_family){
		
		$font_family = str_replace('+', ' ', $font_family);
		$font_family = explode('|', $font_family); //der zweite teil des arrays ist das subset
		if(self::isGoogleFont($font_family[0])){
			return "'".$font_family[0]."',arial,serif";
		}else{
			return $font_family[0];
		}
	}

	public static function prepareFontWeight($font_weight, $ngmenu=false){
		if (preg_match("|(\d+)|", $font_weight, $matches)){ //wenn eine zahl in $font_weight enthalten ist, dann handelt es sich um Google Web Font mit font-weight und font-style
			$weight = 'font-weight:'.$matches[0].';';
			$font_style = str_replace($matches[0], '', $font_weight);
			$style = (empty($font_style)) ? 'font-style:normal;' : 'font-style:'.$font_style.';';
			if($ngmenu){
				$font_style = (empty($font_style)) ? 'normal' : $font_style;
				return $font_style.' '.$matches[0];
//				return $matches[0].' '.$font_style;
			}else{
				return $weight.$style;
			}
		}else{
			if($ngmenu){
				return $font_weight;
			}else{
				return 'font-weight:'.$font_weight.';';
			}
		}
	}
	
	public static function getGoogleFontParms($font_family, $font_weight){
		
		$font_parm = ''; $i = 0; $gfonts = $subsets = array();
		
		if(self::isGoogleFont($font_family)){
			$gfonts[$font_family][] = $font_weight;
		}

		if(count($gfonts)){
			foreach($gfonts as $font_family => $weight_values){
				$font_delimiter = ($i==0) ? '' : '|';
				$font_family = explode('|', $font_family);
				$font_parm .= $font_delimiter.$font_family[0].':'.implode(',', $weight_values);
				$subsets[] = $font_family[1];
				$i+=1;
			}
			if(!empty($font_parm)){
				$subsets = array_unique($subsets);
				$font_parm .= '&subset='.implode(',', $subsets);
			}
		}
		return $font_parm;
	}
}
